package webs.servlet.ServletContext;
//使用ServletContext对象

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/servletContext_one")
public class servletContext_one extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        //1.获取ServletContext对象
        ServletContext context=this.getServletContext();

        //2.获取文件的MIME类型
        String a="a.jpg";
        String contextMimeType=context.getMimeType(a);
        System.out.println(contextMimeType);

        //3.获取文件路径
        String aa="/a.txt";
        String path1=context.getRealPath(aa);//获取web目录下的资源文件
        System.out.println(path1);

        String bb="/WEB-INF/b.txt";//获取web目录下的WEB-INF目录下的文件
        String path2=context.getRealPath(bb);
        System.out.println(path2);

        String cc="/WEB-INF/classes/c.txt";//获取SRC目录下的资源文件，因为SRC目录下的文件会被放到WEB-INF目录下的classes目录
        String path3=context.getRealPath(cc);
        System.out.println(path3);

    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        this.doPost(req,resp);
    }
}
